//
//  HomeView.swift
//  iFishApp
//
//  Created by Bob on 2022/3/4.
//

import SwiftUI
import SDWebImageSwiftUI

struct HomeView: View {
    
    @EnvironmentObject var customTabbarVM: CustomTabbarViewModel
    
    @StateObject var locationManager = LocationManager()
    
    @StateObject var homeVM: HomeViewModel = HomeViewModel()
    
    var body: some View {
        NavigationView {
            GeometryReader { proxy in
                VStack {
                    HomeNavView()
                        .environmentObject(locationManager)
                        .environmentObject(homeVM)
                    ScrollView(.horizontal, showsIndicators: false) {
                        TabView(selection: $homeVM.curHomeNavTab) {
                            Color.red.tag(HomeNavTab.attention)
                            
                            HomeRecommendView()
                                .tag(HomeNavTab.recommend)
                                .environmentObject(homeVM)
                            
                            Color.green.tag(HomeNavTab.location)
                        }
                        .frame(width: proxy.size.width)
                        .frame(maxHeight: .infinity)
                    }
                    .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
                }
                .frame(maxHeight: .infinity, alignment: .top)
                .onAppear {
                    locationManager.manager.requestLocation()
                    customTabbarVM.atFront.toggle()
                }
            }
            .navigationBarHidden(true)
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
